package biz.bbtec.sbm.user.action;

import com.opensymphony.xwork2.ActionSupport;

import biz.bbtec.sbm.user.bean.UserBean;
import biz.bbtec.sbm.user.factory.UserFactory;
import biz.bbtec.sbm.user.service.UserService;

public class RegisterAction extends ActionSupport {
	private UserBean user;

	public UserBean getUser() {
		return user;
	}

	public void setUser(UserBean user) {
		this.user = user;
	}

	public String register() throws Exception {
		if (user.getName().trim().equalsIgnoreCase("")) {
			this.addActionError("真是姓名不能为空！");
			return INPUT;
		}
		if (user.getUser().trim().equalsIgnoreCase("")) {
			this.addActionError("登录名不能为空！");
			return INPUT;
		}
		if (!(user.getPass().trim().equalsIgnoreCase(user.getRepass()))) {
			this.addActionError("两次密码输入不同！");
			return INPUT;
		}
		UserService userservice = UserFactory.getInstance().createUserService();
		if (!(userservice.exit(user))) {
			this.addActionError("您的公司不存在");
			return INPUT;
		} else {
			if (userservice.check(user)) {
				this.addActionError("登录名已被占用，请换一个！");
				return INPUT;
			} else {
				if (userservice.register(user)) {
					this.addActionError("注册成功，请重新登录！");
					return SUCCESS;
				}
				this.addActionError("注册失败，请稍后再试！");
				return INPUT;
			}
		}
	}
}
